﻿using System;
using System.Linq;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.DataLayer.Infra;
using ProjetoComperio.Framework.Web.Security;

namespace ProjetoComperio.BusinessLogicLayer
{
    public class StudentBLL : ComperioDataObject
    {
        #region CRUD

        /// <summary>
        /// Creates a new Student and a new locked user
        /// </summary>
        /// <param name="student"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        public bool Create(Student student, String userName, String password)
        {

            if (String.IsNullOrEmpty(student.WebPage))
                student.WebPage = "http://www.faetec.rj.gov.br/ist-rio";
            DbContext.People.Add(student);
            DbContext.SaveChanges();

            var user = new global::ProjetoComperio.Framework.Web.Security.User();
            user.UserName = userName;
            user.Password = password;
            user.Email = student.Email;
            ComperioMemberShipProvider comperioMemberShipProvider = new ComperioMemberShipProvider();
            user.Role = comperioMemberShipProvider.RetrieveRole(ComperioMemberShipProvider.Roles.Student);
            user.PersonID = student.PersonID;
            if (comperioMemberShipProvider.CreateUser(user) == null)
            {
                Delete(student);
                return false;
            }
            return true;
        }

        public void Delete(Student student)
        {
            DbContext.People.Remove(student);
            DbContext.SaveChanges();
        }
        #endregion

        #region Additional and Related Methods

        public IQueryable<Course> RetrieveCourses()
        {
            return DbContext.Courses;
        }

        public Course RetrieveCourseByID(Int32 courseID)
        {
            return RetrieveCourses().FirstOrDefault(c => c.CourseID == courseID);
        }

        public IQueryable<AcademicOrganization> RetrieveAcademicOrganizations()
        {
            return DbContext.AcademicOrganizations;
        }

        public AcademicOrganization RetrieveAcademicOrganizationByID(Int32 academicOrganizationID)
        {
            return RetrieveAcademicOrganizations().FirstOrDefault(x => x.AcademicOrganizationID == academicOrganizationID);
        }

        /// <summary>
        /// This method returns an educationLevel by its ID
        /// </summary>
        /// <param name="educationLevelID"></param>
        /// <returns></returns>
        public EducationLevel RetrieveEducationLevelByID(Int32 educationLevelID)
        {
            return DbContext.EducationLevels.FirstOrDefault(e => e.EducationLevelID == educationLevelID);
        }

        #endregion



        public Student RetrieveStudentByID(Int32 personID)
        {
            return DbContext.People.OfType<Student>().Where(student => student.PersonID == personID).FirstOrDefault();
        }

        public IQueryable<Student> RetrieveStudents()
        {
            return DbContext.People.OfType<Student>();
        }



    }
}
